Code Meets Culture
April 19, 2025
🔍 Focus of This Session
httr and jsonlite to extract data
| Feature | No Key Required | API Key Required |
|---|---|---|
| Setup Needed | None | Yes (Google Cloud, etc.) |
| Rate Limits | Often lower | Higher limits |
| Use Case | Open data, testing | Production, private APIs |
library(ggplot2)
library(dplyr)
library(viridis)
top_words <- word_freq |>
slice_max(n, n = 99)
# Reorder words for better plot
ggplot(top_words, aes(x = n, y = reorder(word, -n), fill = n)) +
geom_col() +
scale_fill_viridis(option = "D", direction = -1) +
coord_flip() +
labs(
title = "Top 99 Most Frequent Words in the Quran (English Translation)",
x = "Frequency",
y = "Word"
) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90, hjust = 1),
legend.position = "none"
)We used {httr} for flexibility, but there’s also a special R package for YouTube: {tuber} package
{tuber} is a simple wrapper for the YouTube Data API — it makes things easier!
Some key functions include: yt_search() - search for YouTube videos, get_stats() – get video stats like views, likes, and comments and many more.
To use the YouTube Data API, first set your credentials in .Renviron:
Then, in your R script:
⚠️ Note: Some Google APIs (like Google Maps) are paid, so always check pricing to avoid unexpected bills!
# Search for Hari Raya Official Music Video
results <- yt_search("Hari Raya Official Music Video", type = "video", max_results = 50)
# Get stats for each video
stats_list <- lapply(results$video_id, function(id) get_stats(video_id = id))
stats_df <- map_df(stats_list, ~as.data.frame(t(unlist(.x))), .id = "index") video_id publishedAt channelId
1 67p1sOD4e2A 2024-03-08T07:00:08Z UCIxxcbRgejg8scgiqF390jw
2 zf1U4mu0Iew 2016-07-01T13:00:03Z UCSUWBtaz8MVTFy9GULySSmA
3 k5RawzRXeVE 2021-04-09T12:30:15Z UCe05yXcSHjcgZ4EeV8fs_sA
4 4vtwJORy_eU 2013-05-25T01:56:20Z UCBd5pENmJrvi6PQq-2ndBhw
5 seZsq1mD0uk 2021-04-30T12:00:13Z UCAyzn7DznYgmbvvXJ4bC83Q
6 EA_BrwGx8S0 2025-03-19T03:11:20Z UC4Zs5RInEZ2dTBEuvSQvBkg
title
1 Alamak Raya Lagi - De Fam (Official Music Video)
2 Nyanyian Ramai - Warna Warni Aidilftri (Official Music Video)
3 Iman Troye - Pejam Celik Hari Raya (Official Music Video)
4 Siti Nurhaliza - Bila Hari Raya Menjelma (Official Music Video)
5 DOLLA - Raya Raya Raya (Official Music Video)
6 MIMIFLY - SERUMPUN (Official Music Video)
description
1 LISTEN TO ALAMAK RAYA LAGI: https://Faithful.lnk.to/AlamakRayaLagi Maxis Caller Ringtones: Dial *131*774391# and press ...
2 Nyanyian Ramai - Warna Warni Aidilftri Composer - Lan Laga [Universal Music Publishing (M) Sdn Bhd] Lyricist - Amran Omar ...
3 XO House Presents : Iman Troye - Pejam Celik Hari Raya (Official Music Video) Executive Producer = Adib Hamdi Song Producer ...
4 Anggun si dara jelita berselindang dan kebaya Sepadan dengan pemuda tampan dan segak orangnya Di pagi hari mulia ...
5 Stream 'Raya Raya Raya' on all digital streaming platforms: https://dolla.lnk.to/RayaRayaRaya Download RBT now! Maxis Caller ...
6 Composer : Mimifly, Cuurley Author : Mimifly, Cuurley Publisher : Taja Archive Sdn Bhd & 153Joombas Master P&C 2024 MFLY ...
thumbnails.default.url thumbnails.default.width
1 https://i.ytimg.com/vi/67p1sOD4e2A/default.jpg 120
2 https://i.ytimg.com/vi/zf1U4mu0Iew/default.jpg 120
3 https://i.ytimg.com/vi/k5RawzRXeVE/default.jpg 120
4 https://i.ytimg.com/vi/4vtwJORy_eU/default.jpg 120
5 https://i.ytimg.com/vi/seZsq1mD0uk/default.jpg 120
6 https://i.ytimg.com/vi/EA_BrwGx8S0/default.jpg 120
thumbnails.default.height thumbnails.medium.url
1 90 https://i.ytimg.com/vi/67p1sOD4e2A/mqdefault.jpg
2 90 https://i.ytimg.com/vi/zf1U4mu0Iew/mqdefault.jpg
3 90 https://i.ytimg.com/vi/k5RawzRXeVE/mqdefault.jpg
4 90 https://i.ytimg.com/vi/4vtwJORy_eU/mqdefault.jpg
5 90 https://i.ytimg.com/vi/seZsq1mD0uk/mqdefault.jpg
6 90 https://i.ytimg.com/vi/EA_BrwGx8S0/mqdefault.jpg
thumbnails.medium.width thumbnails.medium.height
1 320 180
2 320 180
3 320 180
4 320 180
5 320 180
6 320 180
thumbnails.high.url thumbnails.high.width
1 https://i.ytimg.com/vi/67p1sOD4e2A/hqdefault.jpg 480
2 https://i.ytimg.com/vi/zf1U4mu0Iew/hqdefault.jpg 480
3 https://i.ytimg.com/vi/k5RawzRXeVE/hqdefault.jpg 480
4 https://i.ytimg.com/vi/4vtwJORy_eU/hqdefault.jpg 480
5 https://i.ytimg.com/vi/seZsq1mD0uk/hqdefault.jpg 480
6 https://i.ytimg.com/vi/EA_BrwGx8S0/hqdefault.jpg 480
thumbnails.high.height channelTitle liveBroadcastContent
1 360 FAITHFUL none
2 360 Sony Music MY none
3 360 XO HOUSE none
4 360 SuriaRecords (SRC) none
5 360 DOLLAofficialVEVO none
6 360 Mimifly none
publishTime index
1 2024-03-08T07:00:08Z 1
2 2016-07-01T13:00:03Z 2
3 2021-04-09T12:30:15Z 3
4 2013-05-25T01:56:20Z 4
5 2021-04-30T12:00:13Z 5
6 2025-03-19T03:11:20Z 6
# Data Cleaning
laguhariraya <- stats_df |>
left_join(results |> mutate(index = as.character(row_number())), by = "index") |>
mutate(
index = as.numeric(index),
viewCount = as.numeric(viewCount),
likeCount = as.numeric(likeCount),
commentCount = as.numeric(commentCount),
video_id = video_id,
publishTime = as.POSIXct(publishTime, format = "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
) |>
filter(
!grepl("shorts", tolower(title)),
!grepl("shorts", tolower(description)),
publishTime <= as.POSIXct("2025-03-31", tz = "UTC")
) |>
select(
-favoriteCount,
-starts_with("thumbnails.default"),
-starts_with("thumbnails.medium"),
-starts_with("thumbnails.high"),
-liveBroadcastContent
)
laguhariraya <- laguhariraya %>%
filter(!title %in% c(
"Ngakak 🤣🤣🤣",
"Upin & Ipin hari raya🇲🇾",
"Harry - Suaramu Syairku (Official Music Video)",
"Rajini Murugan Tamil Full Movie - Sivakarthikeyan | Keerthy Suresh | D.Imman | UIE Movies",
"Iman Troye - Teman (Official Music Video)",
"Selamat Hari Raya Idul Fitri 1443 Hijriyah 2022 🙏🙏",
"Selamat Hari Raya!",
"Tahun ni nak dondon lagi tak yeeee? #PapaPipi #InspirasiRaya #SembangRaya #SembangEntertainment",
"kartun lawak kampus raya 2023",
"takbir | takbir keliling 2023 | hari raya idul fitri 2023 | takbiran",
"DISCO HUNTER - Senandung Hari Raya",
"persiapan hari raya idul fitri 2023 tunggu hari mainnya #short #takbiran #desakutuk",
"PETRONAS Raya 2025: Anak Siapa Ni? | Who Could That Be?"
)) index id viewCount likeCount commentCount video_id
1 1 67p1sOD4e2A 56135930 198822 9313 67p1sOD4e2A
2 2 zf1U4mu0Iew 18123175 44014 3381 zf1U4mu0Iew
3 3 k5RawzRXeVE 30995492 228227 14337 k5RawzRXeVE
4 4 4vtwJORy_eU 22446141 58027 4109 4vtwJORy_eU
5 5 seZsq1mD0uk 35572591 191576 112322 seZsq1mD0uk
6 6 EA_BrwGx8S0 8877764 126955 7392 EA_BrwGx8S0
publishedAt channelId
1 2024-03-08T07:00:08Z UCIxxcbRgejg8scgiqF390jw
2 2016-07-01T13:00:03Z UCSUWBtaz8MVTFy9GULySSmA
3 2021-04-09T12:30:15Z UCe05yXcSHjcgZ4EeV8fs_sA
4 2013-05-25T01:56:20Z UCBd5pENmJrvi6PQq-2ndBhw
5 2021-04-30T12:00:13Z UCAyzn7DznYgmbvvXJ4bC83Q
6 2025-03-19T03:11:20Z UC4Zs5RInEZ2dTBEuvSQvBkg
title
1 Alamak Raya Lagi - De Fam (Official Music Video)
2 Nyanyian Ramai - Warna Warni Aidilftri (Official Music Video)
3 Iman Troye - Pejam Celik Hari Raya (Official Music Video)
4 Siti Nurhaliza - Bila Hari Raya Menjelma (Official Music Video)
5 DOLLA - Raya Raya Raya (Official Music Video)
6 MIMIFLY - SERUMPUN (Official Music Video)
description
1 LISTEN TO ALAMAK RAYA LAGI: https://Faithful.lnk.to/AlamakRayaLagi Maxis Caller Ringtones: Dial *131*774391# and press ...
2 Nyanyian Ramai - Warna Warni Aidilftri Composer - Lan Laga [Universal Music Publishing (M) Sdn Bhd] Lyricist - Amran Omar ...
3 XO House Presents : Iman Troye - Pejam Celik Hari Raya (Official Music Video) Executive Producer = Adib Hamdi Song Producer ...
4 Anggun si dara jelita berselindang dan kebaya Sepadan dengan pemuda tampan dan segak orangnya Di pagi hari mulia ...
5 Stream 'Raya Raya Raya' on all digital streaming platforms: https://dolla.lnk.to/RayaRayaRaya Download RBT now! Maxis Caller ...
6 Composer : Mimifly, Cuurley Author : Mimifly, Cuurley Publisher : Taja Archive Sdn Bhd & 153Joombas Master P&C 2024 MFLY ...
channelTitle publishTime year
1 FAITHFUL 2024-03-08 07:00:08 2024
2 Sony Music MY 2016-07-01 13:00:03 2016
3 XO HOUSE 2021-04-09 12:30:15 2021
4 SuriaRecords (SRC) 2013-05-25 01:56:20 2013
5 DOLLAofficialVEVO 2021-04-30 12:00:13 2021
6 Mimifly 2025-03-19 03:11:20 2025
title
1 Lagu Raya | Siti Nordiana x Omar & Hana | Oh Meriahnya Raya! | Video Rasmi
2 Alamak Raya Lagi - De Fam (Official Music Video)
3 Upin & Ipin - Suasana Hari Raya [Sing-Along][HD]
4 DOLLA - Raya Raya Raya (Official Music Video)
5 Floor 88 & Baby Shima - Tak Dapat Raya (Official Music Video)
6 Iman Troye - Pejam Celik Hari Raya (Official Music Video)
7 Siti Nurhaliza - Nazam Lebaran (Official Music Video)
8 Siti Nurhaliza - Air Mata Syawal (Official Music Video)
9 NABILA RAZALI - RAYA MANA? [OFFICIAL MUSIC VIDEO]
10 Balik Kampung Lagi #puasa #animasi #raya
11 Siti Nurhaliza - Bila Hari Raya Menjelma (Official Music Video)
viewCount
1 61970228
2 56135930
3 36157718
4 35572591
5 33519633
6 30995492
7 26399393
8 25870601
9 24893746
10 24164063
11 22446141
title
1 Kalau kalian kegiatan nya di hari raya ngapain aja nih,komen!! #shrots #lucu #komedi #lebaran
2 Balik Kampung Lagi #puasa #animasi #raya
3 Iman Troye - Pejam Celik Hari Raya (Official Music Video)
4 Floor 88 & Baby Shima - Tak Dapat Raya (Official Music Video)
5 Alamak Raya Lagi - De Fam (Official Music Video)
6 DOLLA - Raya Raya Raya (Official Music Video)
7 ECKO SHOW - Takbirap (feat. BOSSVHINO, AIL) [ Music Video ]
8 Hael Husaini - Bersyukur Seadanya [Official Raya Music Video]
9 Upin & Ipin - Suasana Hari Raya [Sing-Along][HD]
10 MIMIFLY - SERUMPUN (Official Music Video)
likeCount
1 607313
2 466882
3 228227
4 223064
5 198822
6 191576
7 157648
8 139022
9 131031
10 126955
title
1 DOLLA - Raya Raya Raya (Official Music Video)
2 Dato Sri Aliff Syukri & Datin Sri Shahida - Kelepok Raya [Official Music Video]
3 Hael Husaini & DOLLA - Suara Lebaran Kita (Official Music Video)
4 Iman Troye - Pejam Celik Hari Raya (Official Music Video)
5 Dato Sri Aliff Syukri - Chu Ku Chuk Raya [Official Music Video] Penyanyi No 1 Malaysia
6 Alamak Raya Lagi - De Fam (Official Music Video)
7 Dato' Sri Siti Nurhaliza - Raya Nak Ke Mana? (Official Music Video)
8 Siti Nurhaliza - Air Mata Syawal (Official Music Video)
9 Hael Husaini - Bersyukur Seadanya [Official Raya Music Video]
10 Menanti Di Aidilfitri - Wany Hasrita, Wani Syaz, Muna Shahirah, Wan Azlyn (Official Music Video)
commentCount
1 112322
2 19263
3 17583
4 14337
5 10471
6 9313
7 8761
8 8724
9 8640
10 7878
Pearson Correlation ≈ 0.93
A strong positive relationship — as views increase, likes increase proportionally.
Pearson Correlation ≈ 0.33
There’s a moderate positive relationship — more liked videos tend to receive more comments, but the relationship is weaker than views vs likes.
Pearson Correlation ≈ 0.40
There is a moderate relationship — videos with higher views tend to have more comments, though not as strongly correlated as likes and views.
The most viewed song released in each year:
Meriah Lain Macam – Hael Husaini & Nadeera
📺 Rocketfuel Network
👁️🗨️ 1,335,263 views · 👍 87,020 · 💬 5,533
Open-Meteo)spotifyr)GNews API)Twitter, TikTok)data.gov)📦 Combine API data with R for storytelling and dashboards
httr, jsonlite, tuber, spotifyrCode Meets Culture: Hari Raya Music Meets R
💬 Any questions?